package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.GoodsSkuBO;
import com.junzhiit.nengji.bo.GoodsSkuItemsBO;
import com.junzhiit.nengji.dao.GoodsSkuItemsDAO;
import com.junzhiit.nengji.model.GoodsSkuItems;

@Repository
public class GoodsSkuItemsDAOImpl extends AbstractBaseDAO<GoodsSkuItems> implements GoodsSkuItemsDAO {

	@Override
	public List<GoodsSkuBO> queryGoodsSku(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select u.id uid,u_item.*");
		sql.append(" from sku u inner join sku_items u_item on u_item.sku_id=u.id");
		sql.append(" where u.shop_id=? and u_item.is_delete=? and u.is_delete=?");
		sql.append(" order by u.created_time desc");

		StatementParameter param = new StatementParameter();
		param.setString(shopId);
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());

		return queryForList(sql.toString(), GoodsSkuBO.class, param);

	}

	@Override
	public List<GoodsSkuItems> queryGoodsSkuItems(String goodsId) {
		String sql = "select * from goods_sku_items where is_delete=? and goods_id=? order by created_time desc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(goodsId);

		return queryForList(sql, GoodsSkuItems.class, param);
	}
	
	@Override
	public List<GoodsSkuItemsBO> queryGoodsSkuItemsBO(String goodsId){
		StringBuffer sql=new StringBuffer();
		sql.append(" select gsi.sku_id,gsi.sku_items_id,gsi.is_open_image,gsi.image,si.standard,s.`name`");
		sql.append(" from goods_sku_items gsi");
		sql.append(" inner join sku s on s.id=gsi.sku_id");
		sql.append(" inner join sku_items si on si.id=gsi.sku_items_id");
		sql.append(" where gsi.goods_id=?");
		sql.append(" order by gsi.sort asc,gsi.sku_items_sort asc");
		
		StatementParameter param=new StatementParameter();
		param.setString(goodsId);
		
		return super.queryForList(sql.toString(), GoodsSkuItemsBO.class,param);
	}

	@Override
	public boolean deleteGoodsSkuItems(String goodsId) {

		String sql = "delete from goods_sku_items where is_delete=? and goods_id=? order by created_time desc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(goodsId);

		return updateForBoolean(sql, param);
	}
	
	
	@Override
	public boolean updateGoodsSkuItemsImage(String imageUrl){
		StringBuffer sql=new StringBuffer();
		sql.append(" update goods_sku_items set image=?");
		sql.append(" where is_delete=? and image=?");
		
		StatementParameter param=new StatementParameter();
		param.setString("");
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(imageUrl);
		
		return updateForBoolean(sql.toString(), param); 
		
	}
}
